Page({
    data: {
      showRoleSelect: false,
      tempCode: '',
      userInfo: null
    },
  
    handleLogin() {
      wx.showLoading({
        title: '登录中...'
      });
  
      // 获取用户信息
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: (userResult) => {
          // 获取登录code
          wx.login({
            success: (loginResult) => {
              if (loginResult.code) {
                // 发送登录请求
                wx.request({
                  url: 'http://localhost:3000/api/auth/login',
                  method: 'POST',
                  data: {
                    code: loginResult.code,
                    userInfo: userResult.userInfo
                  },
                  success: (res) => {
                    wx.hideLoading();
                    
                    if (res.data.success) {
                      if (res.data.data.isNewUser) {
                        // 新用户，显示角色选择
                        this.setData({
                          showRoleSelect: true,
                          tempCode: loginResult.code,
                          userInfo: userResult.userInfo
                        });
                      } else {
                        // 老用户，直接跳转
                        wx.setStorageSync('userInfo', res.data.data.userInfo);
                        this.redirectBasedOnRole(res.data.data.userInfo.role);
                      }
                    } else {
                      wx.showToast({
                        title: '登录失败',
                        icon: 'none'
                      });
                    }
                  },
                  fail: (err) => {
                    wx.hideLoading();
                    wx.showToast({
                      title: '网络错误',
                      icon: 'none'
                    });
                  }
                });
              }
            }
          });
        },
        fail: () => {
          wx.hideLoading();
          wx.showToast({
            title: '获取用户信息失败',
            icon: 'none'
          });
        }
      });
    },
  
    // 选择角色
    handleRoleSelect(e) {
      const role = e.currentTarget.dataset.role;
      
      wx.showLoading({
        title: '设置中...'
      });
  
      wx.request({
        url: 'http://localhost:3000/api/auth/setRole',
        method: 'POST',
        data: {
          code: this.data.tempCode,
          role: role,
          userInfo: this.data.userInfo
        },
        success: (res) => {
          wx.hideLoading();
          if (res.data.success) {
            wx.setStorageSync('userInfo', res.data.data);
            this.redirectBasedOnRole(role);
          } else {
            wx.showToast({
              title: '设置失败',
              icon: 'none'
            });
          }
        },
        fail: () => {
          wx.hideLoading();
          wx.showToast({
            title: '网络错误',
            icon: 'none'
          });
        }
      });
    },
  
    // 根据角色跳转到不同页面
    redirectBasedOnRole(role) {
      if (role === 'teacher') {
        wx.reLaunch({
          url: '/pages/teacher/index'
        });
      } else {
        wx.reLaunch({
          url: '/pages/student/index'
        });
      }
    },
  
    onLoad() {
      // 检查是否已登录
      const userInfo = wx.getStorageSync('userInfo');
      if (userInfo) {
        this.redirectBasedOnRole(userInfo.role);
      }
    }
  });